﻿package org.openChart.meta.manager 
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.filters.GradientGlowFilter;
	import flash.net.URLLoader;
	import flash.net.URLRequest
	import flash.text.StyleSheet;
	import org.openChart.meta.Axis;
	import org.openChart.meta.BarChart;
	import org.openChart.meta.BaseChart;
	import org.openChart.meta.data.GroupSet;
	import org.openChart.meta.Stick;
	import org.openChart.meta.StickLine;
	import org.openChart.meta.Style;
	import org.openChart.meta.TextLabel;
	import org.openChart.meta.XAxis;
	import org.openChart.meta.YAxis;
	import org.openChart.meta.ChartType;
	
	/**
	 * ...
	 * @author Chongyuan
	 */
	public class BarChartBuilder extends XmlBase
	{
		
		public function BarChartBuilder() 
		{
			
		}
		
		/**
		 * Build up bar chart from xml defination.
		 * @param	xml
		 * @return barchart
		 */
		public function buildBarChart( xml:XML, chartType:String ):BarChart
		{
			// Get the default style for chart.
			var object:Object = CSSLoader.instance().getStyle(CSSLoader.chart);
			var style:Style = new Style(object);
			var barChart:BarChart = new BarChart(style);
			
			object = CSSLoader.instance().getStyle(CSSLoader.chart_title);
			style = new Style(object, style);
			
			var title:TextLabel = null;
			if (new String(xml.title) != "")
				title = buildLabel(xml.title[0], style);
			barChart.title = title;
			
			// Build up the x axis.
			var xAxis:Axis = buildAxis(xml.xAxis[0], style);
			barChart.xAxis = xAxis;
			// Build up the y axis.
			var yAxis:Axis = buildAxis(xml.yAxis[0], style);
			barChart.yAxis = yAxis;
			
			if (new String(xml.datas[0]) != "")
			{
				var xmlDataLoader:XmlDataLoader = new XmlDataLoader();
				var labels:Array = xmlDataLoader.buildUpLabels(xml.datas[0]);
				if (labels)
					barChart.xAxis.labels = labels;
				var groupSet:GroupSet = xmlDataLoader.buildGroupSet(xml.datas[0], chartType);
				if (groupSet)
				{
					barChart.groupSet = groupSet;
					// Reset the y axis
					if (groupSet.getMinValue() < yAxis.minValue || groupSet.getMaxValue() > yAxis.maxValue)
					{
						yAxis.minValue = Math.min(groupSet.getMinValue(), yAxis.minValue);
						yAxis.maxValue = Math.max(groupSet.getMaxValue(), yAxis.maxValue);
						yAxis.reInitLabels();
					}
				}
			}
			
			return barChart;
		}
	}
	
}